<?php


namespace App\Controller\Admin;


use App\Annotation\OperationLog;
use App\Controller\AbstractController;
use App\Request\Admin\AdminUserLoginRequest;
use App\Request\Admin\AdminUserSaveRequest;
use App\Request\Admin\OperateLogRequest;
use App\Service\Admin\AdminUserService;
use App\Service\Admin\OperationLogService;
use Hyperf\Di\Annotation\Inject;

class AdminUserController extends AbstractController
{
    /**
     * @Inject()
     * @var AdminUserService
     */
    protected $adminUserService;

    /**
     * @Inject()
     * @var OperationLogService
     */
    protected $operatinLogService;

    /**
     * @OperationLog(event="运营管理-列表",platform="admin")
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function index()
    {
        $where = $this->request->query();
        $list = $this->adminUserService->index($where);
        return $this->response->success($list);
    }

    /**
     * @OperationLog(event="运营管理-添加人员",platform="admin")
     * @param $id
     * @param AdminUserSaveRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function save($id, AdminUserSaveRequest $request)
    {
        $validated = $request->validated();
        $adminUser = $this->adminUserService->save($id, $validated);
        return $this->response->success($adminUser);
    }

    /**
     * @OperationLog(event="运营管理-删除人员",platform="admin")
     * @param $id
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function delete($id)
    {
        $this->adminUserService->delete($id);
        return $this->response->success();
    }

    /**
     * @param AdminUserLoginRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function login(AdminUserLoginRequest $request)
    {
        $validated = $request->validated();
        $loginInfo = $this->adminUserService->login($validated['mobile'], $validated['password'],$validated['captcha_key'],$validated['captcha_code']);
        return $this->response->success($loginInfo);
    }

    /**
     * 获取操作记录
     * @param OperateLogRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function showOperationLog(OperateLogRequest $request)
    {
        $validated = $request->validated();
        $data = $this->operatinLogService->queryLog($validated);
        return $this->response->success($data);
    }
}